<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<!--edge浏览器H5兼容设置-->
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<!--360浏览器H5兼容设置-->
	<meta name="renderer" content="webkit" />
	<title>电脑商城</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--导入核心文件-->
	<script src="../bootstrap3/js/holder.js"></script>
	<link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
	<script src="../bootstrap3/jquery-1.8.3.min.js"></script>
	<script src="../bootstrap3/js/bootstrap.js"></script>
	<script src="../js/menu.js" type="text/javascript" charset="utf-8"></script>
	<script SRC="../js/long.js" type="text/javascript" charset="utf-8"></script>
	<!-- 字体图标 -->
	<link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" />
	<link rel="stylesheet" type="text/css" href="../css/layout.css" />
	<link rel="stylesheet" type="text/css" href="../css/webindex.css" />
	<link rel="stylesheet" type="text/css" href="../css/footer.css" />
	<link rel="stylesheet" type="text/css" href="../css/top.css" />
</head>
<body>
<div class="container">
	<div class="col-md-offset-1 col-md-10">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<p class="panel-title">审核用户信息</p>
			</div>
			<div class="panel-body">
				<form action="orderConfirm.html" role="form">
					<!--购物车表格开始-->
					<table class="cart-table" width="100%">
						<thead>
						<tr>
							<th width="15%">用户id</th>
							<th width="15%">用户名</th>
							<th width="30%">用户类型（0用户 1管理员  2商家）</th>
							<th width="15%">是否通过</th>
							<th width="30%">操作</th>

						</tr>
						</thead>
						<tbody id="user-list" class="cart-body">
						<tr>
							<td>id</td>
							<td>username</td>
							<td>userType</td>
							<td>isPass</td>

							<td>
								<input id="btn-pass-user" type="button"  class="cart-del btn btn-default btn-xs" value="通过" />
								<input id="btn-delete-user" type="button"  class="cart-del btn btn-default btn-xs" value="驳回" />
							</td>
						</tr>
						</tbody>
					</table>
					<div class="total-bar">
					</div>
					<label class="col-md-2 control-label"><a href="index_admin.html">返回</a>&nbsp;&nbsp;&nbsp;</label>
				</form>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">

	$(document).ready(function() {
		showUserList();
	});

	function showUserList() {
		$("#user-list").empty();
		$.ajax({
			url: "/user/check_all_users",
			type: "GET",
			dataType: "JSON",
			success: function(json) {
				let list = json.data;
				console.log("count=" + list.length);
				console.log(json.data);

				let allCount = 0;
				for (let i = 0; i < list.length; i++) {
					let tr = '<tr>' +
							//'<td>#{uid}</td>' +
							'<td>#{username}</td>' +
							'<td>#{userType}</td>' +
							'<td>#{isPass}</td>'+
							'<td>' +
							'<input id="pass-user" onclick="passUser(#{uid})" type="button"  class="cart-del btn btn-default btn-xs" value="通过" />\n' +
							'<input id="delete-user" onclick="rejectUser(#{uid})" type="button"  class="cart-del btn btn-default btn-xs" value="驳回" />\n' +
							'</td>' +
							'</tr>'
					tr = tr.replace(/#{uid}/g, "'"+list[i].uid+"'");
					tr = tr.replace(/#{username}/g, list[i].username);
					// tr = tr.replace(/#{userType}/g, list[i].userType);

					if (list[i].userType == 0) {
						tr = tr.replace(/#{userType}/g, "用户");
					} else if (list[i].userType == 1) {
						tr = tr.replace(/#{userType}/g, "管理员");
					}else if (list[i].userType == 2) {
						tr = tr.replace(/#{userType}/g, "商家");
					}

					tr = tr.replace(/#{isPass}/g, list[i].isPass);

					$("#user-list").append(tr);
					allCount += list[i].num;
				}
			}
		});
	}

	function passUser(uid) {
		console.log("通过"+uid);
		$.ajax({
			url:"http://localhost:8080/user/pass_user",
			type:"POST",
			dataType:"JSON",
			data:{
				"uid":uid
			},
			success:function (json) {
				/*alert(json.state)*/
				if(json.state==200){
					alert("审核通过");
					showUserList();
				}else {
					alert("审核异常")
				}
			},
			error:function (xhr) {
				alert("审核产生未知的异常"+xhr.message);//自动产生错误描述信息
			}
		})
	}

	function rejectUser(uid) {
		console.log("驳回");
		$.ajax({
			url:"/user/"+uid+"/reject_user",
			type:"POST",
			dataType:"JSON",
			success:function (json) {
				/*alert(json.state)*/
				if(json.state==200){
					alert("审核驳回");
					showUserList();
				}else {
					alert("审核异常")
				}
			},
			error:function (xhr) {
				alert("审核产生未知的异常"+xhr.message);//自动产生错误描述信息
			}
		})
	}

</script>
</body>
</html>